/**
 * 
 */
package sg.nus.iss.vmcs.gui.elements;

import java.awt.Label;
import java.awt.event.ActionListener;

import sg.nus.iss.vmcs.controllers.StoreController;
import sg.nus.iss.vmcs.guilisteners.StoreViewerListener;
import sg.nus.iss.vmcs.models.Store;
import sg.nus.iss.vmcs.models.StoreItem;
import sg.nus.iss.vmcs.models.StoreObject;
import sg.nus.iss.vmcs.utils.Constant;
import sg.nus.iss.vmcs.utils.StoreType;

/**
 * Builds a StoreViewer Populated with Store Items
 * Builds using Labelled Displays
 * @author Philip
 *
 */
public class LabelledDisplayStoreViewerBuilder extends StoreViewerBuilder  {

	protected String bannerText;
	protected Store store;
	protected ActionListener listener;
	private StoreController aStoreCtrl;
	private StoreType aType;
	
	/**
	 * Creates a new instance of the Coin Store Viewer Builder
	 * @param sCtrl Reference to Store Controller
	 */
	public LabelledDisplayStoreViewerBuilder(String bannerText, ActionListener listener, Store store) {
		this.bannerText = bannerText;
		this.store = store;
		this.listener = listener;
	}
	
	/* (non-Javadoc)
	 * @see sg.nus.iss.vmcs.gui.elements.StoreViewerBuilder#buildBanner()
	 */
	@Override
	public void buildBanner() {
		Label lblBanner = new Label();
		lblBanner.setText(bannerText);
		lblBanner.setFont(Constant.LABEL_FONT);
		
		storeViewer.setBanner(lblBanner);
	}

	public void setListenerType (StoreController aStoreCtrl, StoreType aType)
	{
		this.aStoreCtrl = aStoreCtrl;
		this.aType = aType;
	}
	
	/* (non-Javadoc)
	 * @see sg.nus.iss.vmcs.gui.elements.StoreViewerBuilder#buildStoreViewerItems()
	 */
	@Override
	public void buildStoreViewerItems() {
		StoreItem[] storeItems = store.getItems();
		LabelledDisplay[] displays = new LabelledDisplay[storeItems.length];
		
		for(int i = 0; i < displays.length; i++) {
			if(aStoreCtrl != null){
				StoreViewerListener aVL = new StoreViewerListener(aType, i, aStoreCtrl);
				StoreObject object = storeItems[i].getContent();
				LabelledDisplay ld = new LabelledDisplay(object.getName(), LabelledDisplay.DEFAULT, new GridItemDisplayStrategy());
				ld.setIndex(i);
				ld.setValue(storeItems[i].getQuantity());
				ld.addListener(aVL);
				storeItems[i].registerObserver(ld);
				displays[i] = ld;
			}
		}
		
		storeViewer.setStoreViewerItems(displays);
	}

	/* (non-Javadoc)
	 * @see sg.nus.iss.vmcs.gui.elements.StoreViewerBuilder#buildListeners()
	 */
	@Override
	public void buildListeners() {
		storeViewer.setListener(listener);
	}
}
